package jUnitPlugin;

import com.supermap.data.ProductType;
import com.supermap.desktop.core.AbstractPlugin;
import com.supermap.desktop.core.Application;
import com.supermap.desktop.core.Bundle;
import com.supermap.desktop.core.event.FormLoadedListener;
import com.supermap.desktop.core.license.LicenseManager;
import socketEnv.Client;
import socketEnv.DataUtil;

import java.io.*;
import java.util.ArrayList;
import java.util.EventObject;

/**
 * @author caolp
 */
public class JUnitGUIPlugin extends AbstractPlugin {
	public JUnitGUIPlugin(String name, Bundle bundle) throws Exception{
		super(name, bundle);
	}

	@Override
	public Boolean load() {
		super.load();
		Application.getActiveApplication().addFormLoadedListener(new FormLoadedListener() {
			@Override
			public void loadFinish(EventObject object) {
					Application.getActiveApplication().getOutput().output("JUnitGUIPlugin Load Finish!");
					//复制properties，方便用例中通过property获取字符串
					try {
						copyProperties();
					} catch (IOException e) {
						e.printStackTrace();
					}
					// 判断系统，如果不是win32就不需要校验了，校验失败会导致robot的mode设置为AWT，最终不会进行mouseMove操作
					String arch = System.getProperty("os.arch").toLowerCase();
					String os = System.getProperty("os.name").toLowerCase();
					if (!(os.contains("win") && arch.contains("32"))) {
						System.setProperty("abbot.robot.verify", "false");
					}
					DataUtil.inti();//初始化测试数据路径
//		           new GetNameThead().start();
//				   new StartThread().start();
//		           开始循环处理服务端的“执行指定测试”的请求
			try {
				Client.connetServer();
			} catch (InterruptedException e1) {
				e1.printStackTrace();
			}
			new Thread(new Runnable() {
				@Override
				public void run() {
					Client.receive();
				}
			}).start();
			}
		});
		return true;
	}

	public void copyProperties() throws IOException {
		String propertiesDir = "./Properties/";
		int pluginCount = Application.getActiveApplication().getPluginManager().getCount();
		ArrayList<String> pluginNames = new ArrayList();
		String pluginName = null;
		for (int i = 0; i < pluginCount; i++) {
			pluginName = Application.getActiveApplication().getPluginManager().get(i).getPluginTitle();
			pluginNames.add(pluginName);
		}
		//以下两个是核心插件，需要单独添加
		pluginNames.add("Core");
		pluginNames.add("Process");
		pluginNames.remove("JUnitGUIPlugin");
		System.out.println(pluginNames.size());
		if (!(new File(propertiesDir).exists())) {
			new File(propertiesDir).mkdir();
		}
		String oldPath = null;
		String newPath = null;
		for (String name : pluginNames) {
			oldPath = System.getProperty("user.dir")+"\\" + name + "\\src\\main\\resources\\" + name + ".properties";
			if (new File(oldPath).exists()) {
				newPath = propertiesDir + name + ".properties";
				copyFile(oldPath, newPath);
			}
		}
	}

	public void copyFile(String oldPath, String newPath) {
		try {
			int byteread = 0;
			InputStream inStream = new FileInputStream(oldPath); //读入原文件
			FileOutputStream output = new FileOutputStream(newPath);
			byte[] buffer = new byte[1444];
			while ((byteread = inStream.read(buffer)) != -1) {
				output.write(buffer, 0, byteread);
				output.flush();
			}
			inStream.close();
		} catch (Exception e) {
			System.out.println("copy file failed!");
			e.printStackTrace();
		}
	}

	@Override
	public boolean isGranted() {
		return LicenseManager.INSTANCE.isGranted(ProductType.IDESKTOPCROSS_ENTERPRISE)
				|| (LicenseManager.INSTANCE.isGranted(ProductType.IDESKTOPCROSS_STANDARD)
				&& LicenseManager.INSTANCE.isGranted(ProductType.IDESKTOPCROSS_ONLINESHARE));
	}

	@Override
	public String getPluginTitle() {
		return "JUnitGUIPlugin";
	}
}
